package com.dd.cloud.user.service.goods.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.IntegralConstants;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.utils.ExtUtils;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.user.entity.doctor.DoctorIntegral;
import com.dd.cloud.user.entity.goods.ConversionGood;
import com.dd.cloud.user.mapper.goods.ConversionGoodMapper;
import com.dd.cloud.user.req.goods.ConversionGoodReq;
import com.dd.cloud.user.res.goods.ConversionGoodExcel;
import com.dd.cloud.user.service.doctor.IDoctorIntegralService;
import com.dd.cloud.user.service.goods.IConversionGoodService;
import com.itextpdf.kernel.color.Lab;
import jodd.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 兑换商品表 服务实现类
 * </p>
 *
 * @author czg
 * @since 2024-01-18
 */
@Service
public class ConversionGoodServiceImpl extends ServiceImpl<ConversionGoodMapper, ConversionGood> implements IConversionGoodService {

    @Override
    public Page<ConversionGood> queryConversionGoodRecordPage(ConversionGoodReq req) {
        if (StringUtil.isBlank(req.getStatus())) {
            req.setStatus("1,2,3");
        }
        if (StringUtil.isBlank(req.getTypes())) {
            req.setTypes("1,2,3");
        }
        Page<ConversionGood> page = req.pagePojo();
        page = this.getBaseMapper().queryConversionGoodRecordPage(page, req);
        return page;
    }

    @Override
    public ConversionGood queryConversionGoodRecordDetail(Integer id) {
        return this.getById(id);
    }

    @Autowired
    private IDoctorIntegralService doctorIntegralService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendConversionGoods(Integer id, Integer status) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        ConversionGood conversionGood = this.getById(id);
        if (conversionGood == null) {
            throw new ZekeException("记录不存在");
        }
        conversionGood.setStatus(status)
                .setUpdateDate(LocalDateTime.now())
                .setUpdateName(loginInfo.getName());
        Boolean flag = this.updateById(conversionGood);
        if (!flag) {
            throw new ZekeException("更新失败");
        }
        if (status == 3) {
            String orderNo = conversionGood.getOrderNo();
            LambdaQueryWrapper<DoctorIntegral> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DoctorIntegral::getExchangeNo, orderNo)
                    .eq(DoctorIntegral::getStatus, 3);
            DoctorIntegral doctorIntegral = doctorIntegralService.getOne(queryWrapper);
            if (doctorIntegral != null) {
                DoctorIntegral nwqDoctorIntegral = new DoctorIntegral();
                BeanUtils.copyProperties(doctorIntegral, nwqDoctorIntegral);
                doctorIntegral.setStatus(2).setCreateDate(LocalDateTime.now())
                        .setTitle("兑换积分退回")
                        .setType(IntegralConstants.IntegralType.TWENTY_THREE);
                Boolean flag1 = doctorIntegralService.save(doctorIntegral);
                if (!flag1) {
                    throw new ZekeException("积分退回失败");
                }
            }
        }
    }

    @Override
    public void exportConversionGoods(ConversionGoodReq req, HttpServletResponse response) {
        List<ConversionGoodExcel> list = this.getBaseMapper().exportConversionGoods(req);
        String title = "积分兑换列表";
        try {
            ExtUtils.exportExcel(list, title, title, ConversionGoodExcel.class, title, true, response);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ZekeException("导出失败");
        }
    }

    @Override
    public Page<ConversionGood> queryConversionGoodPage(ConversionGoodReq req) {
        if (StringUtil.isBlank(req.getStatus())) {
            req.setStatus("1,2");
        }
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        Integer doctorId = loginInfo.getId();
        req.setDoctorId(doctorId);
        Page<ConversionGood> page = req.pagePojo();
        page = this.getBaseMapper().queryConversionGoodRecordPage(page, req);
        return page;
    }
}
